package com.xjscrm.console.web.materialLibrary;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xjscrm.common.entity.Merchant;
import com.xjscrm.common.entity.MerchatChild;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.common.result.ResponseCode;
import com.xjscrm.console.param.BaseDto;
import com.xjscrm.console.param.materialLibrary.*;
import com.xjscrm.console.param.user.ChildMerchatDto;
import com.xjscrm.console.service.common.user.UserService;
import com.xjscrm.console.service.customer.childUser.ChildMerchatService;
import com.xjscrm.console.service.customer.materialLibrary.MaterialLibraryService;
import com.xjscrm.console.service.customer.materialLibrary.MaterialLibrarybBtchService;
import com.xjscrm.console.vo.MaterialConfigVo;
import com.xjscrm.console.vo.MaterialLibraryTagVo;
import com.xjscrm.console.vo.MaterialVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Api(value = "素材库管理", tags = "素材库管理")
@RestController
@RequestMapping(value = "/materialLibrary")
public class MaterialLibraryController {

    @Autowired
    private MaterialLibraryService materialLibraryService;

    @Autowired
    private MaterialLibrarybBtchService materialLibrarybBtchService;

    @Autowired
    private ChildMerchatService childMerchatService;

    @Autowired
    private UserService userService;


    @ApiOperation(value = "新增素材标签", notes = "新增素材标签", response = Long.class)
    @PostMapping("/addMaterialLibraryTag")
    @ApiOperationSupport(order = 1)
    public Object addMaterialLibraryTag(@RequestBody MaterialLibraryTagDto MaterialLibraryTagDto) throws Exception {
        getUpdateUserName(MaterialLibraryTagDto);
        return materialLibraryService.addMaterialLibraryTag(MaterialLibraryTagDto);
    }

    @ApiOperation(value = "修改素材标签", notes = "修改素材标签", response = Boolean.class)
    @PostMapping("/updateMaterialLibraryTag")
    @ApiOperationSupport(order = 2)
    public Object updateMaterialLibraryTag(@RequestBody MaterialLibraryTagDto MaterialLibraryTagDto) throws Exception {
        getUpdateUserName(MaterialLibraryTagDto);
        materialLibraryService.updateMaterialLibraryTag(MaterialLibraryTagDto);
        return true;
    }


    @ApiOperation(value = "查询素材标签列表", notes = "查询素材标签列表", response = MaterialLibraryTagVo.class)
    @PostMapping("/getMaterialLibraryTagList")
    @ApiOperationSupport(order = 3)
    public Object getMaterialLibraryTagList(@RequestBody MaterialLibraryTagQuery materialLibraryTagQuery) throws Exception {
        return materialLibraryService.getMaterialLibraryTagList(materialLibraryTagQuery);
    }

    @ApiOperation(value = "批量删除素材标签", notes = "批量删除素材标签", response = Boolean.class)
    @PostMapping("/batchdelMaterialLibraryTag")
    @ApiOperationSupport(order = 4)
    public Object batchdelMaterialLibraryTag(@RequestBody BatchdelMaterialLibraryDto batchdelMaterialLibraryDto) throws Exception {
        materialLibrarybBtchService.batchdelMaterialLibraryTag(batchdelMaterialLibraryDto);
        return true;
    }


    @ApiOperation(value = "获取素材标签下拉", notes = "获取素材标签下拉", response = Map.class)
    @PostMapping("/getMaterialLibraryTagDropDown")
    @ApiOperationSupport(order = 5)
    public Object getMaterialLibraryTagDropDown(@RequestBody MaterialLibraryTagDto MaterialLibraryTagDto) throws Exception {
        return materialLibraryService.getMaterialLibraryTagDropDown(MaterialLibraryTagDto);
    }


    @ApiOperation(value = "新增素材", notes = "新增素材", response = Boolean.class)
    @PostMapping("/addMaterialLibrary")
    @ApiOperationSupport(order = 6)
    public Object addMaterialLibrary(@RequestBody MaterialDto materialDto) throws Exception {
        getUpdateUserName(materialDto);
        materialLibraryService.addMaterialLibrary(materialDto);
        return true;
    }


    @ApiOperation(value = "修改素材", notes = "修改素材", response = Boolean.class)
    @PostMapping("/updateMaterialLibrary")
    @ApiOperationSupport(order = 7)
    public Object updateMaterialLibrary(@RequestBody MaterialDto materialDto) throws Exception {
        getUpdateUserName(materialDto);
        materialLibraryService.updateMaterialLibrary(materialDto);
        return true;
    }


    @ApiOperation(value = "查询素材列表", notes = "查询素材列表", response = MaterialVo.class)
    @PostMapping("/getMaterialLibraryList")
    @ApiOperationSupport(order = 8)
    public Object getMaterialLibraryList(@RequestBody MaterialQuery materialQuery) throws Exception {
        return materialLibraryService.getMaterialLibraryList(materialQuery);
    }


    @ApiOperation(value = "查询素材列表详情", notes = "查询素材列表详情", response = MaterialVo.class)
    @PostMapping("/getMaterialLibraryDetail")
    @ApiOperationSupport(order = 9)
    public Object getMaterialLibraryDetail(@RequestBody MaterialDto materialDto) throws Exception {
        return materialLibraryService.getMaterialLibraryDetail(materialDto);
    }


    @ApiOperation(value = "批量删除素材", notes = "批量删除素材", response = Boolean.class)
    @PostMapping("/batchDelMaterialLibrary")
    @ApiOperationSupport(order = 10)
    public Object batchDelMaterialLibrary(@RequestBody BatchdelMaterialLibraryDto batchdelMaterialLibraryDto) throws Exception {
        materialLibrarybBtchService.batchDelMaterialLibrary(batchdelMaterialLibraryDto);
        return true;
    }


    @ApiOperation(value = "修改重要素材", notes = "修改重要素材", response = Boolean.class)
    @PostMapping("/updateImportant")
    @ApiOperationSupport(order = 11)
    public Object updateImportant(@RequestBody UpdateImportantDto updateImportantDto) throws Exception {
        materialLibraryService.updateImportant(updateImportantDto);
        return true;
    }

    @ApiOperation(value = "新增素材配置", notes = "新增素材配置", response = Boolean.class)
    @PostMapping("/addMaterialConfig")
    @ApiOperationSupport(order = 12)
    public Object addMaterialConfig(@RequestBody MaterialConfigDto materialConfigDto) throws Exception {
        materialLibraryService.addMaterialConfig(materialConfigDto);
        return true;
    }

    @ApiOperation(value = "修改素材配置", notes = "修改素材配置", response = Boolean.class)
    @PostMapping("/updateMaterialConfig")
    @ApiOperationSupport(order = 13)
    public Object updateMaterialConfig(@RequestBody MaterialConfigDto materialConfigDto) throws Exception {
        materialLibraryService.updateMaterialConfig(materialConfigDto);
        return true;
    }

    @ApiOperation(value = "查询素材配置", notes = "查询素材配置", response = MaterialConfigVo.class)
    @PostMapping("/getMaterialConfig")
    @ApiOperationSupport(order = 14)
    public Object getMaterialConfig(@RequestBody BaseDto baseDto) throws Exception {
        return materialLibraryService.getMaterialConfig(baseDto);
    }


    public void getUpdateUserName(MaterialDto materialDto) {
        if (materialDto.getOperatorType() != null && materialDto.getOperatorType() == 0 && materialDto.getMerId() != null) {
            BaseDto baseDto = new BaseDto();
            baseDto.setMerId(materialDto.getMerId());
            Merchant merchant = userService.getById(baseDto);
            if (merchant == null) {
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该主账号不存在");
            }
            materialDto.setUpdateUser(merchant.getUserName());
        }

        if (materialDto.getOperatorType() != null && materialDto.getOperatorType() == 1 && materialDto.getMerId() != null && materialDto.getChildId() != null) {
            ChildMerchatDto childMerchatDto = new ChildMerchatDto();
            childMerchatDto.setMerId(materialDto.getMerId());
            childMerchatDto.setId(materialDto.getChildId());
            MerchatChild merchatChild = childMerchatService.getById(childMerchatDto);
            if (merchatChild == null) {
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该子账号不存在");
            }
            materialDto.setUpdateUser(merchatChild.getUserName());
        }
    }





    public void getUpdateUserName(MaterialLibraryTagDto materialLibraryTagDto) {
        if (materialLibraryTagDto.getOperatorType() != null && materialLibraryTagDto.getOperatorType() == 0 && materialLibraryTagDto.getMerId() != null) {
            BaseDto baseDto = new BaseDto();
            baseDto.setMerId(materialLibraryTagDto.getMerId());
            Merchant merchant = userService.getById(baseDto);
            if (merchant == null) {
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该主账号不存在");
            }
            materialLibraryTagDto.setUpdateUser(merchant.getUserName());
        }

        if (materialLibraryTagDto.getOperatorType() != null && materialLibraryTagDto.getOperatorType() == 1 && materialLibraryTagDto.getMerId() != null && materialLibraryTagDto.getChildId() != null) {
            ChildMerchatDto childMerchatDto = new ChildMerchatDto();
            childMerchatDto.setMerId(materialLibraryTagDto.getMerId());
            childMerchatDto.setId(materialLibraryTagDto.getChildId());
            MerchatChild merchatChild = childMerchatService.getById(childMerchatDto);
            if (merchatChild == null) {
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该子账号不存在");
            }
            materialLibraryTagDto.setUpdateUser(merchatChild.getUserName());
        }
    }
}
